﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI
{
    public partial class ProductsInfoForm : Form
    {
        private ProductsBLL _productsBLL = new ProductsBLL();
        public ProductsInfoForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            InitData();
        }

        public void InitData()
        {
            //初始化商品类别数据
            dgvCategoryInfo.DataSource = _productsBLL.SelectProductsCategory();

            //初始化商品信息数据
            dgvProductInfo.DataSource = _productsBLL.SelectProductsInfo();
        }

        private void dgvCategoryInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取选中行的类别id
            int id = Convert.ToInt32(dgvCategoryInfo.SelectedRows[0].Cells[0].Value);

            //调用BLL
            dgvProductInfo.DataSource = _productsBLL.SelectProductsInfoByID(id);
        }

        private void btnShowAllProductInfo_Click(object sender, EventArgs e)
        {
            //初始化商品信息
            dgvProductInfo.DataSource = _productsBLL.SelectProductsInfo();
        }

        private void btnAddCategory_Click(object sender, EventArgs e)
        {
            UpdateProductsCategoryForm updateProductsCategoryForm = new UpdateProductsCategoryForm();
            updateProductsCategoryForm.Text = "添加类别";
            updateProductsCategoryForm.ShowDialog();
            //初始化商品类别数据
            if(updateProductsCategoryForm.DialogResult == DialogResult.OK)
            {
                dgvCategoryInfo.DataSource = _productsBLL.SelectProductsCategory();
            }
            

        }


        //发起方定义事件用于传递数据
        public event EventHandler CategoriesChanged;
        private void btnUpdateCategory_Click(object sender, EventArgs e)
        {
            UpdateProductsCategoryForm updateProductsCategoryForm = new UpdateProductsCategoryForm();
            updateProductsCategoryForm.Text = "修改类别";

            //获取类别信息
            ProductsCategory productsCategory = new ProductsCategory();
            productsCategory.CatId = Convert.ToInt32(dgvCategoryInfo.SelectedRows[0].Cells[0].Value);
            productsCategory.CatName = dgvCategoryInfo.SelectedRows[0].Cells[1].Value.ToString();
            productsCategory.Remark = dgvCategoryInfo.SelectedRows[0].Cells[2].Value.ToString();
            productsCategory.SubTime = Convert.ToDateTime(dgvCategoryInfo.SelectedRows[0].Cells[4].Value);

            CategoriesChanged += updateProductsCategoryForm.GetValue;

            CategoriesChanged.Invoke(this, productsCategory);
            updateProductsCategoryForm.ShowDialog();
            //初始化商品类别数据
            if (updateProductsCategoryForm.DialogResult == DialogResult.OK)
            {
                dgvCategoryInfo.DataSource = _productsBLL.SelectProductsCategory();
            }
        }

        private void benDeleteCategory_Click(object sender, EventArgs e)
        {
            ProductsCategory productCategory = new ProductsCategory();
            productCategory.CatId = Convert.ToInt32(dgvCategoryInfo.SelectedRows[0].Cells[0].Value);
            //调用删除BLL
            if(_productsBLL.UpdateCategory(productCategory, 2) > 0)
            {
                MessageBox.Show("删除成功");
                //初始化商品类别数据
                dgvCategoryInfo.DataSource = _productsBLL.SelectProductsCategory();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            //调用BLL搜索
            dgvProductInfo.DataSource = _productsBLL.SeleceProductsInfoByLike(txtSearch.Text);

        }

        private void btnAddPro_Click(object sender, EventArgs e)
        {
            //ProductsInfo productsInfo = new ProductsInfo();
            //updateProductsCategoryForm.Text = "添加类别";
            //updateProductsCategoryForm.ShowDialog();
            ////初始化商品类别数据
            //if (updateProductsCategoryForm.DialogResult == DialogResult.OK)
            //{
            //    dgvCategoryInfo.DataSource = _productsBLL.SelectProductsCategory();
            //}
        }
    }


    
}
